<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1">
    <meta name="referrer" content="never">
    <title>旋转木马</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }

        .box {
            position: relative;
            width: 613px;
            height: 230px;
            margin: 150px auto;
        }

        .box li {
            position: absolute;
        }

        li:first-child, li:last-child {
            top: -88px;
            left: -75px;
            width: 300px;
            opacity: .4;
            z-index: 5;
        }

        li:last-child {
            left: 470px;
        }

        li:nth-child(2), li:nth-child(4) {
            top: 0;
            left: -200px;
            width: 460px;
            z-index: 10;
            opacity: .7;
        }

        li:nth-child(4) {
            left: 415px;
        }

        li:nth-child(3){
            top: 70px;
            z-index: 20;
        }

        .box li img {
            width: 100%;
        }

        button{
            margin-top: 20px;
            margin-left: 100px;
            padding: 12px 40px;
            font-size: 16px;
            letter-spacing: .1rem;
            background-color: #fff;
            border-radius: 3px;
            border: 1px solid #999;
        }
    </style>
</head>
<body>
    <ul class="box">
        <li><img src="../lib/image/bom/m1.jpg" alt=""></li>
        <li><img src="../lib/image/bom/m2.jpg" alt=""></li>
        <li><img src="../lib/image/bom/m3.jpg" alt=""></li>
        <li><img src="../lib/image/bom/m4.jpg" alt=""></li>
        <li><img src="../lib/image/bom/m5.jpg" alt=""></li>
    </ul>

    <button>往左转</button>
    <button>往右转</button>

    <script type="text/javascript">
        // 设置样式数组    
        var arr = [
            {
                top: -88,
                left: -75,
                width: 300,
                opacity: .4,
                zIndex: 5
            },
            {
                top: 0,
                left: -200,
                width: 460,
                opacity: .7,
                zIndex: 10
            },
            {
                top: 70,
                left: 0,
                width: 613,
                opacity: 1,
                zIndex: 20
            },
            {
                top: 0,
                left: 415,
                width: 460,
                opacity: .7,
                zIndex: 10
            },
            {
                top: -88,
                left: 470,
                width: 300,
                opacity: .4,
                zIndex: 5
            },
        ]

        var lis = document.querySelectorAll(".box li");

        var btns = document.querySelectorAll("button");

        btns[0].onclick = function (){
            // 往左转  将样式最后一个放到第一个
            arr.unshift(arr.pop());
            for(var i = 0; i < lis.length; i ++){
                animate(lis[i], arr[i]);
            }
        }

        btns[1].onclick = function (){
            // 往右转  将样式数组第一对象放到数组最后一个，对应li赋值
            arr.push(arr.shift());
            for(var i = 0; i < lis.length; i ++){
                animate(lis[i], arr[i]);
            }
        }

        function animate(ele, params){
            // params => { width: 600, height: 600, opacity: .3 ...}
            // 判断是否用定时器，有清除
            if(ele.timer){
                clearInterval(ele.timer);
            }
            ele.timer = setInterval(function (){
                var start, end;
                // 所有样式都到达终点表示 true，假设都到达了终点
                var flag = true;
                console.log("=================");
                for(var i in params){
                    // console.log("样式名：", i, "样式值", params[i]);
                    // 透明度 -- 起点，终点放大，赋值时缩小
                    if(i == "opacity"){
                        start = parseInt(getComputedStyle(ele, null)[i] * 100);
                        end = params[i] * 100;
                    }else if (i == "zIndex"){
                        ele.style.zIndex = params[i];
                        continue; // break;
                    }else {
                        start = parseInt(getComputedStyle(ele, null)[i]);
                        end = params[i];
                    }

                    // 设置步长
                    var step = (end - start)/10;
                    if(Math.abs(step) < 1){
                        step = step > 0 ? 1 : Math.floor(step);
                    }

                    // 赋值
                    if(i == "opacity"){
                        ele.style.opacity = (start + step)/100;
                    }else {
                        ele.style[i] = start + step + "px";
                    }

                    if(start + step !== end){
                        // 有一个没有到达终点，整体状态改为false
                        flag = false;
                        console.log("没有到达终点的属性", i);
                    }
                }

                // 假设成真
                if(flag){
                    clearInterval(ele.timer);
                    ele.timer = null;
                }
            }, 1000/60)
        }
    </script>
</body>
</html>